package com.sky.city.interceptor;

import android.util.Log;

import com.common.baselibrary.request.RequestBean;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Locale;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class LogInterceptor implements Interceptor {
    private String TAG = "LogInterceptor";

    @NotNull
    @Override
    public Response intercept(@NotNull Chain chain) throws IOException {
        Request request = chain.request();
        Log.e(TAG, "request:" + request.toString());
        long t1 = System.nanoTime();
        Response response = chain.proceed(chain.request());
        long t2 = System.nanoTime();
        Log.e(TAG, String.format(Locale.getDefault(), "Received response for %s in %.1fms%n%s",
                response.request().url(), (t2 - t1) / 1e6d, response.headers()));
        okhttp3.MediaType mediaType = response.body().contentType();
        String content = response.body().string();
        Log.e(TAG, "response body:" + content);
        RequestBean requestBean = new Gson().fromJson(content, RequestBean.class);
        if (requestBean.getCode() == 606
                || requestBean.getCode() == 607
                || requestBean.getCode() == 603
                || requestBean.getCode() == 609
                || "登陆失效".equals(requestBean.getMsg())
                || "登陆异常".equals(requestBean.getMsg())) {//重新登录
//            LoginUtilsManager.newInstance().startLogin();
        }
        return response.newBuilder()
                .body(okhttp3.ResponseBody.create(mediaType, content))
//                .header("Authorization", Your.sToken)
                .build();
    }

}
